#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-10-25 15:41:14
# Project: anjuke_com_album

import json
import html
import time
import random
import requests
from pyspider.libs.base_handler import *
from pyquery import PyQuery as pq

from src.mysqldb import SQL
from src.headers_switch import HeadersSelector
from src.utils import download_img
from src.utils import md5
from src.utils import oss
from src.utils import get_proxy


class Handler(BaseHandler):
    crawl_config = {
        'itag': 'vsn0.0.2',
        'proxy': '',
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl_config['proxy'] = get_proxy()
        header_slt = HeadersSelector()
        header = header_slt.select_header()  # 获取一个新的 header
        header['Referer'] = 'https://dg.fang.anjuke.com/loupan/xiangce-258337/jst.html?from=click_xiangcelist_tab_shijing'
        header['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
        # self.crawl('__START_URL__', callback=self.index_page)
        items = [
            (414421, [
                # ('画报', 'hb'),
                # ('样板间', 'ybj'),
                ('实景图', 'jst'),
                ('效果图', 'xgt'),
                ('规划图', 'ght'),
                ('配套图', 'ptt'),
                ('交通图', 'wzt'),
            ]),
            (440330, [
                ('效果图', 'xgt'),
                ('交通图', 'wzt'),
            ]),
            (258337, [
                # ('画报', 'hb'),
                ('样板间', 'ybj'),
                ('实景图', 'jst'),
                ('效果图', 'xgt'),
                ('规划图', 'ght'),
                ('配套图', 'ptt'),
                ('交通图', 'wzt'),
            ]),
            (240349, [
                ('样板间', 'ybj'),
                ('实景图', 'jst'),
                ('效果图', 'xgt'),
                ('规划图', 'ght'),
                ('配套图', 'ptt'),
                ('交通图', 'wzt'),
            ]),
        ]
        js_script = """
            function() {
                setTimeout(function() {}, 3000);
            }
        """
        url = 'https://sh.fang.anjuke.com/loupan/xiangce-%d/%s.html'
        for (house_id, tag_li) in items:
            for (tag, tag2) in tag_li:
                save = {
                    'house_id': house_id,
                    'tag': tag,
                    'tag2': tag2,
                }
                self.crawl(url % (house_id, tag2), callback=self.detail_page, save=save, headers=header, fetch_type='js', js_script=js_script, connect_timeout=5000, timeout=20000)

    @config(priority=2)
    def detail_page(self, response):
        self.crawl_config['proxy'] = get_proxy()
        header_slt = HeadersSelector()
        header = header_slt.select_header()  # 获取一个新的 header

        header['Referer'] = 'https://dg.fang.anjuke.com/loupan/xiangce-258337/jst.html?from=click_xiangcelist_tab_shijing'
        header['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'

        doc = response.doc
        save = response.save
        house_id = save.get('house_id', '0')
        tag = save.get('tag', '')
        tag2 = save.get('tag2', '')

        pic_list = [img.attr('src') for img in doc('.xiangce-list .o-list .o-item img').items()]
        print(pic_list)
        #return
        # 下载图片到 oss
        subpath = 'goods_house/album_%d/%s' % (house_id, tag2,)
        sql = SQL()
        for thumb in pic_list:
            img_url = thumb.replace('200x150m', '860x10000')
            origin_url = img_url
            thumb = oss(subpath, thumb, headers=header)
            img_url = oss(subpath, img_url, headers=header)
            result = {
                "house_id": house_id,
                "tag": tag,
                "img_url": img_url,
                "thumb": thumb,
                "origin_url": origin_url,
            }
            sql.replace('jjb_goods_house_album', result)
        return {}

    def on_result(self,result):
        print(result)
        if not result or not result.get('house_id', ''):
            return
        sql = SQL()
        header = result.get('header', None)
        if header:
            result.pop('header')
        # print(result)
        sql.replace('jjb_goods_house_album', result)
